// pages/mySelf/Index/index.js
const common = require('../../../public.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    selected:4,
    mess_num: 0,
    userInfo: null,
    token: '',
    // past: true,
    type: 2,
    userAssets: 0, //余额
    discountCouponNums: 0, //优惠卷
    benefitCouponNums: 0, // 惠民券
    storeFavolist: 0, //收藏
    totalStoreDrawOrderNums: 0, // 抽奖订单总数
    storeDrawOrder: '', // 订单详情
    storeDrawOrderFlag: '', // 是否开奖
    nianList: [],
  },
  getUserAssets() { // 获取余额
    const that = this
    const tempJson = {
      userId: that.data.userInfo.id,
      token: that.data.token
    }
    common.post('/userDeposit/getUserAssetsGold', tempJson, function (res) {
      if (res.status === 200) {
        that.setData({
          userAssets: res.data
        })
      } else {
        common.showRequestErrMsg(res)
        that.setData({
          userAssets: res.data
        })
      }
    })
  },
  onShow() {
    const that = this
   
    //获取胶囊位置
    let capsule = wx.getMenuButtonBoundingClientRect()
    this.setData({
        height: capsule.height,
        top: capsule.top,
    })
    var windowHeight = wx.getSystemInfoSync().windowHeight
    var windowWidth = wx.getSystemInfoSync().windowWidth
    this.setData({
        scroll_height: windowHeight * 750 / windowWidth - 200
    })
    common.getLocalUserInfo(function (success, data) {
      if (success) {
        console.log(data,"datadatadatadata")
        that.setData({
          userInfo: data.user,
          token: data.token
        }, function () {
          // that.getActList();
          // that.getCode()
          that.getUserCenterInfo()
          this.read_message()
          that.cardsList()
          that.getUserAssets()
        })
      } else {
        that.setData({
          userInfo: null,
          token: '',
        })
      }
    })
  },
  cardsList() {
    var that = this
    common.get('/userAnnualCard/getUserCardProductList', {
      userId: that.data.userInfo.id,
      token: that.data.token
    }, res => {
      if (res.status === 200) {
        that.setData({
          nianList: res.data
        })
      } else {
        common.showRequestErrMsg(res)
      }
    })
  },
  //跳转到book
  toPackBook() {
    wx.navigateTo({
      url: '../../../packageA/pages/',
    })
  },
  // 演出订单
  click_yan() {
    wx.navigateTo({
      // url="{{'../../Goods/goodsOrder/goodsOrderList/index?type=1'}}"
      url: '/packageA/pages/performs/performOrderList/performOrderList?type=1',
    })
  },

  // 统计用户未读消息
  read_message() {
    var that = this
    var tmp = {
      userId: this.data.userInfo.id,
      token: this.data.token
    }
    common.get('/alipush/getUserNotReadMessageCount', tmp, res => {
      if (res.status === 200) {
      } else {
        common.showRequestErrMsg(res)
      }

    })
  },
  // 跳转会员卡订单
  toNianOrder() {
    wx.navigateTo({
      url: '/pages/ScenicArea/NianCardOrder/index',
    })
  },
  //跳转到个人信息页面
  toPeopleInfo() {
    wx.navigateTo({
      url: '../peopleInfo/peopleInfo'
    })
  },
  toLogin() { // 去登陆
    common.showLoginPage()
  },

  applyEnter() { // 点击申请入驻
    common.showToast('请登录飞羊精选app查看')
  },
  contactServer() { //联系客服
    wx.navigateTo({
      url: '/packageB/pages/custService/index',
    })
  },
  getUserCenterInfo() { // 获取用户信息
    if (this.data.userInfo === null) {
      return
    }
    let that = this
    let tempJson = {
      userId: that.data.userInfo.id,
      token: that.data.token
    }

    common.getJson("/user/getUserCenterInfo", tempJson, function (res) {
      if (res.status === 200) {
        console.log('res', res)
        that.setData({
          // userAssets: parseFloat(res.data.userAssets).toFixed(2),
          discountCouponNums: res.data.discountCouponNums,
          storeFavolist: res.data.storeFavolist,
          totalStoreDrawOrderNums: res.data.totalStoreDrawOrderNums,
          storeDrawOrder: res.data.storeDrawOrder ? res.data.storeDrawOrder : '',
          storeDrawOrderFlag: res.data.storeDrawOrderFlag ? res.data.storeDrawOrderFlag : '',
          benefitCouponNums: res.data.benefitCouponNums
        })
      } else {
        common.showRequestErrMsg(res)
      }
    }, function (responseType) {
      if (responseType === 1) {
        // 登录过期
        that.clearState()
      }
    })
  },
  clearState() {
    this.setData({
      userInfo: null,
      token: '',
      userAssets: 0,
      discountCouponNums: 0,
      // storeFavolist: [],  //收藏
      storeFavolist: 0,
      totalStoreDrawOrderNums: 0,
      storeDrawOrder: '',
      storeDrawOrderFlag: '',
      benefitCouponNums: 0
    })
  },
  updateData() {
    const that = this
    wx.getStorage({
      key: 'user',
      success: function (res) {
        that.setData({
          userInfo: res.data.user,
          token: res.data.token,
        }, function () {
          // that.getActList();
          // that.getCode()
          that.getUserCenterInfo()
        })
      }
    })
  },
  
  toCollection() { // 跳转收藏
    this.checkLoginToJump('/pages/collection/index')
  },
  toWallet() { // 跳转钱包
    // this.checkLoginToJump('/pages/mySelf/wallet/index')
    this.checkLoginToJump('/packageB/pages/Goldbeans/Goldbeans')
  },
  toCoupons() { // 跳转优惠券中心
    // this.checkLoginToJump('/pages/coupons/myCoupons/index')
    wx.navigateTo({
      url: '/pages/coupons/myCoupons/index',
    })
  },
  toCultureCoupons() {
    this.checkLoginToJump('/pages/cultureCoupons/myCultureCoupons/index')
  },
  toDraw() {
    // common.showToast('请移步飞羊精选app')
    this.checkLoginToJump('/packageB/pages/my_lucky/index')
  },
  toAddressManager() {
    this.checkLoginToJump('/packageA/pages/Goods/Address/list/index?from=mySelf')
  },
  /**
   * 跳转到实名认证页面
   */
  shop_shang() {
    wx.navigateTo({
      // url="{{'../../Goods/goodsOrder/goodsOrderList/index?type=1'}}"
    //   url: '/packageA/pages/Goods/goodsOrder/goodsOrderList/index?type=1',
    url: '/packageA/pages/Goods/goodsOrder/goodsOrderList/index?type=1',
    })
  },
  toRealName() {
    console.log(this.data.userInfo)
    if (this.data.userInfo && this.data.userInfo.isRealname) {
    //   common.showToast('您已实名')
      wx.navigateTo({
        url: '../authentication/authentication',
      })
    } else {
      this.checkLoginToJump('/pages/realIdent/index')
    }
  },
  
  toNianKa() {
    wx.navigateTo({
      url: '/packageC/pages/packCard/packCard',
      // url:'packageC/pages/packCard/packCard'
    })
  },
  /**
   * 跳转页面，该接口会验证用户是否登录
   * @param {string} url 跳转连接
   */
  checkLoginToJump(url) {
    if (!this.data.userInfo) {
      common.showToast('您尚未登录，请登录后再查看吧~')
    } else {
      wx.navigateTo({
        url
      })
    }
  },
  toVip() {
    this.checkLoginToJump('/pages/vip/index')
  },
  logout() {
    wx.removeStorageSync('user')
    common.showToast('您已退出登录')
    this.clearState()
  }
})